<?php
namespace Home\Model;
use Think\Model;
class BonusModel extends Model {

	protected $set=array();
	public function setBonus(){
		$this->set=include(dirname(APP_PATH) . '/User/Home/Conf/settings.php');
	}
	protected function log($str){

	}
	 function tj($p_id){

		$tg_info=M('tgbz')->where(array('id'=>$p_id,'zt'=>1,'qr_zt'=>2))->find();if(empty($tg_info)){$this->log('');return;}

		//判断是否完成全部支付完成
		$money=$tg_info['jb'];
		$pd_money=M('ppdd')->where(array('p_id'=>$tg_info['id']))->sum('jb');
		if($money!=$pd_money){return;}

		//获取打款人信息
		$uinfo=getuserinfo($tg_info['user']);if(empty($uinfo)){return;}

		//获取
		$order=$uinfo['user_latematch'];if($order>0){$this->log('期数错误！');return;}

		if($this->set['tj_num']<=0){return;}

		//获取满足要求的推荐人列表
		$list=$this->get_tj_list($uinfo['ue_accname'],$order);if($count($list)<=0){return;}
		foreach ($list as $key=>$tj_info){
			$username=$tj_info['ue_account'];
//			slog($username);

			if($key==1){
				//验证第一代推荐人在本期是否拿到过推荐奖
				slog('验证第一代推荐人在本期是否拿到过推荐奖');
				if($this->check_tj_ceng($username,$order,1)==false){continue;}

				//获取在本期推荐人下提供帮助最小金额
				$min=$this->get_tj_min($username,$order);

				//验证此次是否金额是否最小值
				if($min<$money){continue;}
			}

			//写入
			$this->write_tj($username,$tg_info['id'],$money,$key,$order,$tg_info['user']);
		}
	}
	 function write_tj($username,$tg_id,$money,$ceng,$order,$fromuser){
		$bl=$this->get_tj_bl($ceng);
		$data['user']=$username;
		$data['date']= date ( 'Y-m-d H:i:s', time () ); //操作时间
		$data['zt']=1;
		$data['jb']=$money;
		$data['r_id']=$tg_id;
		$data['jj']=$money*$bl;
		$data['order']=$order;
		$data['ceng']=$ceng;
		$data['fromuser']=$fromuser;
		$data['ds']=1;
		$data['note']='推荐奖'. ($bl*100) .'%';
		M('user_jl')->add($data);

		M('user')->where(array('UE_account'=>$username))->save(array('jb',$money));

//		$record3 ["UG_account"] = $username; // 登入转出账户
//		$record3 ["UG_type"] = 'jb';
//		$record3 ["UG_allGet"] = $accname_zq['tj_he']; // 金币
//		$record3 ["UG_money"] = '+'.$money; //
//		$record3 ["UG_balance"] = $accname_xz['tj_he']; // 当前推荐人的金币馀额
//		$record3 ["UG_dataType"] = 'tjj'; // 金币转出
//		$record3 ["UG_note"] = $note3; // 推荐奖说明
//		$record3["UG_getTime"]= date ( 'Y-m-d H:i:s', time () ); //操作时间
//		$reg4 = M ( 'userget' )->add ( $record3 );
	}

	//获取满足要求的推荐人列表
	protected function get_tj_list($tj_username,$order){
		$tj_num=$this->set['tj_num'];
		if($tj_num<=0){return null;}

		$list=array();
		$tinfo=getuserinfo($tj_username);
		$ceng=1;

		while (!empty($tinfo) ){
			$latemtch=$tinfo['user_latematch'];
			$fist=$tinfo['user_firstmatch'];
			$count=$tinfo['user_nummatch'];
			$run_num=$this->set['tj_run'][$ceng];

			//验证是否参加本期匹配数
			if($latemtch!=$order){
				$ceng++;$tinfo=getuserinfo($tinfo['ue_accname']);
				continue;
			}

			//是否提供帮助申请的推荐人
			$pd_tg_info=M('ppdd')->where(array('p_user'=>$tj_username,'order'=>$order))->find();
			if(empty($pd_tg_info)){
				$ceng++;$tinfo=getuserinfo($tinfo['ue_accname']);
				continue;
			}

			//验证次数
			if($count>$run_num){
				$ceng++;$tinfo=getuserinfo($tinfo['ue_accname']);
				continue;
			}

			//验证是否连续
			if(($latemtch-$fist)!=($count-1)){
				$ceng++;$tinfo=getuserinfo($tinfo['ue_accname']);
				continue;
			}

			$list[$ceng]=$tinfo;
			$ceng++;

			//限制层数
			if($ceng>$tj_num){return $list;}

			$tinfo=getuserinfo($tinfo['ue_accname']);//推荐人信息
		}
		return $list;
	}


	protected function get_tj_bl($ceng){
		switch ($ceng){
			case 1:
				return 0.1;
			case 2:
				return 0.02;
			case 3:
				return 0.01;
			case 4:
			case 5:
			case 6:
			case 7:
			case 8:
				return 0.005;
			default:
				return 0;
		}
	}

	//验证用户是否在参加本期
	protected function check_tj_join($username,$order){
		$uinfo=M('user')->where(array('ue_account'=>$username,'user_latematch'=>$order));
		if(empty($uinfo)){
			return false;
		}
		else{
			return true;
		}
	}

	//验证用户在本期是否拿到过推荐奖
	protected function check_tj_ceng($username,$order,$ceng){
		$info=M('user_jl')->where(array('user'=>$username,'order'=>$order,'ceng'=>$ceng));
		if(empty($info)){
			return false;
		}else{
			return true;
		}
	}

	//获得推荐人拿到次数
	protected  function get_tj_count($tj_username,$fromuser,$order){
		$count=M('user_jl')->where(array('user'=>$tj_username,'$fromuser'=>$fromuser,'order'=>$order));
		return $count;
	}

	//获取用户所有直推人在该期提供帮助金额最小值
	protected  function get_tj_min($username,$order){

		$list=M('user')->table('ot_user user,ot_tgbz tgbz')->where('user.ue_account=tgbz.user')
			->where(array('user.ue_account'=>$username,'user.user_latematch'=>$order,'tgbz.tg_order'=>$order))
			->select();
		dump(count($list));
		slog($list);
//		$ulist=M('user')->where(array('ue_account'=>$username,'user_latematch'=>$order));

		$min=$list[0]['jb'];
		foreach ($list as $info){
			if($info['jb']<$min){
				$min=$info['jb'];
			}
		}
		dump($min);
		return $min;
	}
}
?>